﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Enities;
using BLToolkit.Data;
using System.Collections;
using DAL;
using System.Linq.Expressions;
using System.Data.SqlClient;


namespace QLThuVien_Nhom21
{
    public partial class DSPhieuNhap : Form
    {
        private bool _chon;
        public List<Exception> Exceptions { get; set; }
        private IGeneralRepository<PhieuNhap> _repository1;
        private IGeneralRepository<ChiTietPhieuNhap> _repository2;
        public DSPhieuNhap()
        {
            InitializeComponent();
        }
        
        private void btnThemPN_Click(object sender, EventArgs e)
        {
            QLNhapTaiLieu pn = new QLNhapTaiLieu();
            pn.Show();

        }

        private void btnThemCTPN_Click(object sender, EventArgs e)
        {
            ThemChiTietPhieuNhap ct = new ThemChiTietPhieuNhap();
            ct.Show();
        }

        private void btnTimTheoTNL_Click(object sender, EventArgs e)
        {
            //TimTheoNguoiLapPhieu nl = new TimTheoNguoiLapPhieu();
            //nl.Show();
            _repository1 = new GeneralRepository<PhieuNhap>();
            TimTheoNguoiLapPhieu dialog = new TimTheoNguoiLapPhieu();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string sName = dialog.GetCondition();
                List<PhieuNhap> listpn = _repository1.Find(t => t.NguoiLap.Equals(sName)).ToList();
                dtgPhieuNhap.DataSource = listpn;

            }
        }
        public bool Success
        {
            get { return !Exceptions.Any(); }
        }
        private void btnTimTheoMaPN_Click(object sender, EventArgs e)
        {
            //TimTheoMaPhieuNhap m = new TimTheoMaPhieuNhap();
            //m.Show();
            _repository1 = new GeneralRepository<PhieuNhap>();
            TimTheoMaPhieuNhap dialog = new TimTheoMaPhieuNhap();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string sName = dialog.TimKiemTheoMa();
                List<PhieuNhap> listpn = _repository1.Find(t => t.MaPhieuNhap == sName).ToList();
                if (listpn.Count==0)
                { MessageBox.Show("Phieu nhap khong ton tai"); }
                else
                {

                    // var sResult = _repository1.Find(s => s.MaPhieuNhap.IndexOf(sName) >= 0);
                    List<ChiTietPhieuNhap> listt = _repository2.Find(ct => ct.MaPhieuNhap==sName).ToList();
                    if (listt.Count == 0)
                    {
                        MessageBox.Show("khoong co chi tiets");
                    }

                    dtpChiTietPN.DataSource = listt;
                    dtpChiTietPN.Refresh();
                }
            }
        }

        public void DsPhieuNhap()
        {
            ArrayList list = new ArrayList();
            PhieuNhap pn;
            using (DbManager db = new DbManager())
            {
                db.SetCommand("SELECT * FROM PhieuNhap");

                using (IDataReader rd = db.ExecuteReader())
                {
                    while (rd.Read())
                    {
                        // ...
                        pn = new PhieuNhap();
                        pn.MaPhieuNhap = rd.GetString(0);
                        pn.NguoiLap = rd.GetString(1);
                        //pn.NgayLap =ConvertType.ToDateTime(rd.GetDateTime(2));

                        list.Add(pn);
                    }
                    dtgPhieuNhap.DataSource = list;
                }
            }
        
        }

        private void DSPhieuNhap_Load(object sender, EventArgs e)
        {
            //DsPhieuNhap();
        }

        private void btnDS_Click(object sender, EventArgs e)
        {
            _repository1 =new GeneralRepository<PhieuNhap>();
            _repository2 = new GeneralRepository<ChiTietPhieuNhap>();
            dtgPhieuNhap.DataSource = _repository1.All().ToList();
            dtpChiTietPN.DataSource = _repository2.All().ToList();
        }

        private void dtgPhieuNhap_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && dtgPhieuNhap.Rows.Count > 0)
            {
                 _chon = !_chon;
                 for (int idx=0; idx < dtgPhieuNhap.Rows.Count; idx++)
                 {
                     dtgPhieuNhap.Rows[idx].Selected = _chon;
                     dtgPhieuNhap.Rows[idx].Cells[0].Value = _chon;
                 }
            }
        }

        private void dtgPhieuNhap_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dtgPhieuNhap.Rows[e.RowIndex].Cells[0].Value != null)
            {
                bool chon = (bool)dtgPhieuNhap.Rows[e.RowIndex].Cells[0].Value;
                dtgPhieuNhap.Rows[e.RowIndex].Selected = chon;
            }

        }

        private void deletePhiếuNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _repository1 = new GeneralRepository<PhieuNhap>();
            foreach (DataGridViewRow row in dtgPhieuNhap.Rows)
            {
                if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true))
                    _repository1.Delete(t =>t.MaPhieuNhap==(dtgPhieuNhap.SelectedCells[1].Value).ToString());
                
            }
            dtgPhieuNhap.DataSource = _repository1.All().ToList();
        }

        private void btnXoaPN_Click(object sender, EventArgs e)
        {
            //_repository1 = new GeneralRepository<PhieuNhap>();

            //_repository1.Delete(t => t.MaPhieuNhap = txtMaPN.Text.ToString());           
            _repository1 = new GeneralRepository<PhieuNhap>();
            string mapn = txtMaPN.Text;
            PhieuNhap pn = new PhieuNhap();
            List<PhieuNhap> listpn=_repository1.Find(t=>t.MaPhieuNhap==mapn).ToList();
            if(!listpn.Exists(t=>t.MaPhieuNhap==mapn))
            {
                MessageBox.Show("Phieu nhap khong tin tai");                
            }
            else
            {

                List<ChiTietPhieuNhap> listt = _repository2.Find(t => t.MaPhieuNhap == mapn).ToList();
                if (MessageBox.Show(this, "Phiếu nhập tồn tại chi tiết, Xóa?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
              {
                  bool kq = _repository1.Delete(t => t.MaPhieuNhap == mapn);
                for (int i = 0; i < listt.Count; i++ )
                {                   
                        _repository2.Delete(t => t.MaPhieuNhap == mapn);
                        dtpChiTietPN.DataSource = _repository2.All().ToList();
                        dtgPhieuNhap.DataSource = _repository1.All().ToList();
                    }

              }
               
            }              
            
        }

        private void dtgPhieuNhap_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int n = e.RowIndex;
            try
            {
                txtMaPN.Text = dtgPhieuNhap.Rows[n].Cells[1].Value.ToString();
            }
            catch
            {
                MessageBox.Show("chon lai du lieu ");
            }
        }

        private void dtgPhieuNhap_CurrentCellChanged(object sender, EventArgs e)
        {
            try
            {
                
                int r = this.dtgPhieuNhap.CurrentCell.RowIndex;
                int c = this.dtgPhieuNhap.CurrentCell.ColumnIndex;
                txtMaPN.Text= dtgPhieuNhap.Rows[r].Cells[c].Value.ToString();
            }
            catch { }
        }

        private void btnxoachitiet_Click(object sender, EventArgs e)
        {
            _repository2 = new GeneralRepository<ChiTietPhieuNhap>();
            int mapn =Convert.ToInt32(txtmachitiet.Text);
            bool kq = _repository2.Delete(t => t.MaCTPN == mapn);
            if (kq == true)
            {
                MessageBox.Show("Xóa chi tiết phiếu nhập thành công");
                dtpChiTietPN.DataSource = _repository2.All().ToList();
            }
            else
            {
                MessageBox.Show("xóa ci tiết phiếu  nhập không thành công");
            }
        }
       

        

        
    }
}
